const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ProvinceAndCity:[],
    buxian:0,
    ProList:[],
    btncurr:false,
    box:1,
    type:1,
    btnName:'完成',
    bjsel:0,
    quanguo:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type:options.type
    })
    var that =this;
    this.fns(function(adds){        
      for (var i = 0; i < adds.length; i++) {
        adds[i].sel = 0;
      }
      that.setData({
        ProvinceAndCity:adds
      }) 
    })
  },
  fns(fn){
    var ProAndCity = wx.getStorageSync('ProvinceAndCityKey');
    if (ProAndCity) {
      if(fn){
        fn(ProAndCity);
      }
    }else{
      wx.request({
        url: app.data.api_url+"api/FeijiuApp/GetCProvinceAndCity?strRSA="+app.data.strRSA,
        success: function(res) {
          if (res.statusCode == 200) {
            var addr1 = res.data.data
            var addr2 = [{pcid: "0", code: "110000", name: "城市不限"}]
            for (var i = 0; i < addr1.length; i++) {
              addr1[i].list.unshift(addr2[0])
            }
            if(fn){
              fn(addr1);
            }                     
            wx.setStorageSync('ProvinceAndCityKey',addr1)
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  selectPr:function (e) {
    var that = this;
    var index = e.currentTarget.dataset.idx;    
    var prv = this.data.ProvinceAndCity;
    var Prolist = this.data.ProList;
    if (this.data.buxian == 1) {
      this.setData({
        buxian:0
      })
    }
    if (this.data.type == 3) {
      for (var i = 0; i < prv.length; i++) {
        prv[i].sel = 0
      }   
      this.setData({
        bjsel:index,
        quanguo:0
      }) 
      Prolist.push(index);   
    }else{
      if (Prolist.indexOf(index) > -1) {
        Prolist.splice(Prolist.indexOf(index),1);
      }else{
        Prolist.push(index);      
      }
    }
    if (Prolist.length > 0) {
      this.setData({
        btncurr:true
      })
    }else{
      this.setData({
        btncurr:false
      })      
    }
    if (prv[index].sel == 1) {
      prv[index].sel = 0;
    }else{
      prv[index].sel = 1;
    }
    if (Prolist.length == 1 && this.data.type == 1) {
        this.setData({
          btnName:'选择市区'
        })
    }else{
        this.setData({
          btnName:'完成'
        })      
    }
    this.setData({
      ProvinceAndCity:prv
    })
  },
  shengbuxian:function () {
    var buxian = this.data.buxian;
    var that = this;
    if (buxian == 0) {
      if (this.data.ProList.length > 0) {
        wx.showModal({
          title: '尊敬的用户',
          content: '是否要选择不限,选择不限后其他选项将被清空',
          success (res) {
            if (res.confirm) {
              var prv = that.data.ProvinceAndCity;
              for (var i = 0; i < prv.length; i++) {
                prv[i].sel = 0
              }
              that.setData({
                buxian:1,
                ProvinceAndCity:prv,
                ProList:[],
                btncurr:true
              })
            } 
          }
        })
      }else{
        that.setData({
          buxian:1,
          btncurr:true
        })        
      }
    }else{
      that.setData({
        buxian:0,
        // ProvinceAndCity:[],
        ProList:[],
        btncurr:false
      })
    }
  },
  //确定,如果box=1并只选择了一个省则进入选择市
  queding:function () {    

    if (this.data.type == 1 &&  this.data.ProList.length == 1) {
      wx.navigateTo({
        url: 'selectCity?proId='+this.data.ProList[0]+'&box='+this.data.type
      })
    }else{
      var provcity = this.data.ProvinceAndCity;
      var ProList = this.data.ProList;
      var prov = '';
      var provname = '';
      var cityname = '不限';
      var city = '0';
      if (this.data.type == 3) {
        if (this.data.quanguo == 1) { 
          prov = '0';
          provname = '全国';
        }else{
          provname = provcity[this.data.bjsel].name;
          prov = provcity[this.data.bjsel].pcid;
        }
      }else {
        if (this.data.buxian == 1) {
          prov = '0';
          provname = '不限';
        }else{
          for (var i = 0; i < ProList.length; i++) {
            provname = provname+provcity[ProList[i]].name+',';
            prov = prov+provcity[ProList[i]].pcid+',';
          }
        }
        provname = (provname.substring(provname.length-1)==',')?provname.substring(0,provname.length-1):provname;
        prov = (prov.substring(prov.length-1)==',')?prov.substring(0,prov.length-1):prov;
      }
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2];    
      prevPage.setData({
        prov:prov,
        provname:provname,
        cityname:cityname,
        city:city,
        provnamecname:provname
      })
      wx.navigateBack({
         delta: 1,
      })      
    }

  },
  hq_quanguo:function () {
    var prv = this.data.ProvinceAndCity;
    var Prolist = this.data.ProList;    
    for (var i = 0; i < prv.length; i++) {
      prv[i].sel = 0
    } 
    this.setData({
      quanguo:1,
      bjsel:0,
      ProvinceAndCity:prv,
      btncurr:true,
    }) 
    
  }
})
